<?php

/**
 * @author Thibaut Cromphaut, Gaïtano Boeraeve, Mathias Helin, Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Artevelde University College Ghent 
 */


abstract class Ahs_Model_Mapper extends Ahs_Model
{

    /**
     * @var Zend_Db_Table_Abstract
     */
    protected $_dbTable;
    /**
     * @var string
     */
    protected $_dbTableName;
    /**
     * @var array
     */
    protected $_propertiesMap = array();
    
    /**
     * @return string 
     */
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable($this->_dbTableName);
        }
        return $this->_dbTable;
    }
    /**
     * @param string $dbTable
     * @return Ahs_Model_Mapper 
     */
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
    
    /**
     * @return string 
     */
    public function getDbTableName()
    {
        return $this->_dbTableName;
    }
    /**
     * @param string $dbTableName 
     */
    public function setDbTableName($dbTableName)
    {
        $this->_dbTableName = $dbTableName;
    }
    
    /**
     * PropertiesMap
     * Bind the Object's Properties to its database-column
     * 
     * @return Array
     */
    public function getPropertiesMap()
    {
        return $this->_propertiesMap;
    }
    /**
     * @param array $propertiesmap;
     */
    public function setPropertiesMap($propertiesMap)
    {
        $this->_propertiesMap = $propertiesMap;
    }
    
    /**
     *
     * @param string $property
     * @return string databasecolumn 
     */
    public function getColumn($property)
    {
        if(isset($this->_propertiesMap[$property]))
        {
            return $this->_propertiesMap[$property];
        }
        
        // If we get here, we need to push an error, the porperty requested does not exist
        throw new Exception(get_class($this).'->getColumn("'.$property.'") -> Invalid property name');
    }
    
    public function getLoggedInUserId()
    {
        $storage = Zend_Auth::getInstance()->getStorage()->read();
        return $storage->Id;
    }
    
    /**
     * GetTableValues
     * 
     * Returns a Mapped array of the object's properties and the database columns.
     * 
     * @param Application_Model_* $object
     * @param Array(Properties) $skipProperties
     * @return Array $dataArray
     */
    protected function _getTableValues($object, $skipProperties=null)
    {
        $dataArray          = array();
        if(!is_array($skipProperties))
            $skipProperties     = array();
        
        if(count($this->PropertiesMap))
        {
            foreach($this->PropertiesMap as $propertyKey=>$columnKey)
            {
                // First, determine the value
                $value  = $object->$propertyKey;
                
                // Value should not be "NULL"
                if($value != null)
                {
                    // We don't want to save just any property
                    if(!in_array($propertyKey, $skipProperties))
                    {
                        // Some properties require special treatment
                        /*
                        switch($propertyKey)
                        {
                            default:
                                break;
                        }
                        */
                        // If we are here, it's safe to assign a value to our dataArray
                        $dataArray[$columnKey]   = $value;
                    }
                }
            }
        }
        
        return $dataArray;
    }
}
?>
